package weather.connector.data;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import weather.connector.WeatherServiceConnector;
import weather.connector.exceptions.DateNotSupportedException;

public class ForecastData 
{
	private List<WeatherInfo> informations;
	private String query;
	
	private String date;
	
	public static final int TODAY = 0;
	public static final int TOMMORROW = 1;
	public static final int IN_TWO_DAYS = 2;
	public static final int IN_THREE_DAYS = 3;
	public static final int IN_FOUR_DAYS = 4;
	public static final int IN_FIVE_DAYS = 5;
	
	private ForecastData()
	{
	}
	public String getQuery() 
	{
		return query;
	}
	public boolean isUpToDate()
	{
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
	    String currentDate = sdf.format(cal.getTime());
	    return currentDate.equals(date);
	}
	public static ForecastData prepareForecast(String query)
	{
		ForecastData fd = null;
		WeatherServiceConnector wsc = new WeatherServiceConnector();
		Document doc;
		try 
		{
			doc = wsc.getForecast(query);
			List<WeatherInfo> wi = wsc.createWeatherInfo(doc);
			if(wi.size()!=6)
				return null; //TODO moze lepiej wyrzucic wyjatek
			fd = new ForecastData();
			fd.query = query;
			fd.informations = wi;
			fd.date = wi.get(0).getDay()+wi.get(0).getMonth()+wi.get(0).getYear();
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (ParserConfigurationException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (SAXException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (XPathExpressionException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return fd;
	}
	private int normalizeDate(int date) 
	{
		if(date<TODAY)
			date = TODAY;
		else if(date > IN_FIVE_DAYS )
			date = IN_FIVE_DAYS;
		return date;
	}
	public String getHighTemperature(int date2) throws DateNotSupportedException 
	{
		if(date2<TODAY||date2 > IN_FIVE_DAYS)
			throw new DateNotSupportedException();
		date2 = normalizeDate(date2);
		return informations.get(date2).getHighTemp();
	}
	public String getLowTemperature(int date2) throws DateNotSupportedException 
	{
		if(date2<TODAY||date2 > IN_FIVE_DAYS)
			throw new DateNotSupportedException();
		date2 = normalizeDate(date2);
		return informations.get(date2).getLowTemp();
	}
	public String getWeatherCondition(int date2) throws DateNotSupportedException 
	{
		if(date2<TODAY||date2 > IN_FIVE_DAYS)
			throw new DateNotSupportedException();
		date2 = normalizeDate(date2);
		return informations.get(date2).getConditions();
	}
	public String getSkyIcon(int date2) 
	{
		date2 = normalizeDate(date2);
		return informations.get(date2).getSkyIcon();
	}

}
